﻿using System;
using System.Web.Services;
using HiTeachers.Entities;
using HiTeachers.Services;

namespace HiTeachers.Web.Payment
{
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://hiteachers.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service : WebService
    {
        [WebMethod]
        public void UpdateOrder(string transaction_info, string order_code, string payment_id, string payment_type, string secure_code)
        {
            string secureCodeWs = GetMd5Hash(transaction_info + " " + order_code + " " + payment_id + " " + payment_type + " " + NlCheckout.SecurePass);

            if (!secureCodeWs.Equals(secure_code)) return;
            PaymentInfo paymentInfo = PaymentInfoServices.GetByOrderCode(order_code);
            if (paymentInfo == null) return;

            //Teacher teacher = paymentInfo.Teacher;
            //DateTime fromDate = teacher.DeactivatedDate < DateTime.Now ? DateTime.Now : teacher.DeactivatedDate;
            //DateTime toDate = fromDate.AddMonths(paymentInfo.Period);

            //teacher.IsFullAccount = true;
            //teacher.ActivatedDate = fromDate;
            //teacher.DeactivatedDate = toDate;
            //TeacherServices.SaveOrUpdate(teacher);

            paymentInfo.Status = "1".Equals("payment_type") ? PaymentStatus.Success : PaymentStatus.Pending;//PaymentStatus.Success;
            PaymentInfoServices.SaveOrUpdate(paymentInfo);

            //TODO:Send Email to user
            //NlCheckout.SendNotityPaymentEmail(teacher, paymentInfo.Period.ToString(), DateTime.Now);
        }

        [WebMethod]
        public void RefundOrder(string transaction_info, string order_code, string payment_id, string refund_payment_id, string payment_type, string secure_code)
        {
            string secureCodeWs = GetMd5Hash(transaction_info + " " + order_code + " " + payment_id + " " + refund_payment_id + " " + NlCheckout.SecurePass);

            if (!secureCodeWs.Equals(secure_code)) return;
            PaymentInfo paymentInfo = PaymentInfoServices.GetByOrderCode(order_code);
            if (paymentInfo == null) return;

            Teacher teacher = paymentInfo.Teacher;
            DateTime atcDate = teacher.ActivatedDate.AddMonths(-paymentInfo.Period);
            DateTime deActDate = teacher.DeactivatedDate.AddMonths(-paymentInfo.Period);

            teacher.ActivatedDate = atcDate;
            teacher.DeactivatedDate = deActDate;
            teacher.IsFullAccount = (DateTime.Now <= deActDate);
            TeacherServices.SaveOrUpdate(teacher);

            paymentInfo.Status = PaymentStatus.Failed;
            PaymentInfoServices.SaveOrUpdate(paymentInfo);

            //TODO:Send email to user.
        }

        #region Private Methods
        private static String GetMd5Hash(string input)
        {
            var x = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);

            bs = x.ComputeHash(bs);

            var s = new System.Text.StringBuilder();

            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            String md5String = s.ToString();
            return md5String;
        }
        #endregion
    }
}
